<template>
  <Panel>
    <!-- 标题 -->
    <template #title>修改密码</template>
    <!-- 内容 -->
    <template
      ><el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原密码" prop="uname">
          <el-input
            type="text"
            v-model="ruleForm.uname"
            autocomplete="off"
            @blur="checkPwd()"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="checkPass">
          <el-input
            type="text"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="affirmcheckPass">
          <el-input
            type="text"
            v-model="ruleForm.affirmcheckPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form></template
    >
  </Panel>
</template>

<script>
// 引入
import {
  validatePass2,
  valinewPass,
  valiCheckPass
} from '../../utils/valide.js'

import { checkOldPass, resetPass } from '../../api/user.js'
import local from '../../utils/local.js'

export default {
  data() {
    return {
      ruleForm: {
        uname: '',
        checkPass: '',
        affirmcheckPass: ''
      },
      rules: {
        uname: [{ validator: validatePass2, trigger: 'blur' }],
        checkPass: [
          { validator: valinewPass('新密码', this), trigger: 'blur' }
        ],
        affirmcheckPass: [
          { validator: valiCheckPass('确认新密码', this), trigger: 'blur' }
        ]
        /* age: [{ validator: checkAge, trigger: 'blur' }] */
      },
      passFlag: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          /*  alert('submit!') */
          if (this.passFlag) {
            // 发请求
            const { code, msg } = await resetPass({
              newPwd: this.ruleForm.checkPass
            })
            /* console.log(data) */
            if (code === 0) {
              this.$message({
                type: 'success',
                message: msg
              })
              // 清除token刷新
              setTimeout(() => {
                local.cls()
                location.reload()
              }, 1500)
            }
          } else {
            this.$message.error('原密码是错误的')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async checkPwd() {
      if (this.ruleForm.uname === '') return
      const { code, msg } = await checkOldPass({
        oldPwd: this.ruleForm.uname
      })
      /*  console.log(data) */
      if (code === '00') {
        this.$message({
          type: 'success',
          message: msg
        })
        this.passFlag = true
      } else {
        this.$message({
          type: 'error',
          message: msg
        })
        this.passFlag = false
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
